/**
 *
 * @return
 */
var default_Option = {

    table_Id: "#listTable",
    head: [],
    searchURL: "",
    searchType: "json",
    $table: {},
    table_Option: {},
    $search: {},
    operation: {
        useable: true,
        edit: true,
        "delete": true
    },
    queryFn: function () {

        var $table = default_Option.$table,
            $search = default_Option.$search,
            pageSize = $("#QBar_Page").find(".current").attr("value"),
            pageNum = $table.getPageNum(),
            keyName = $("#QBar_SearchKey").find("a.current").attr("value"),
            keyValue = $("#QBar_Text").val().trim();

        $search.setPageNum(pageNum);
        $search.setPageSize(pageSize);
        $search.addCondition(keyName, keyValue);
        if ($table.getOrderBy() && "operation" != $table.getOrderBy()) {
            $search.addCondition($table.getOrderBy(), null, null, $table.getOrder());
        }
        $search.doSearch();
    },
    successFn: function (_result) {

        var row;
        for (var i = 0, len = _result.data.list.length; i < len; i += 1) {
            row = _result.data.list[i];

            if (!default_Option.operation.useable) continue;
            row["operation"] = "";
            if (default_Option.operation.edit) {
                var $a = $("<a/>").attr("href", default_Option.editURL + "?id=" + row["id"]).html("[编辑]");
                row["operation"] = $a.prop("outerHTML");
            }

            if (default_Option.operation["delete"]) {
                var $a = $("<a/>").attr("href", "javascript:default_Option.rowDeleteFn(" + row["id"] + ")").html("[删除]");
                row["operation"] += "&nbsp;" + $a.prop("outerHTML");
            }

            if (typeof(default_Option.operation["other"]) == 'string') {
                row["operation"] += "&nbsp;" + default_Option.operation["other"].replace("{row_id}", row["id"]);
            } else if (typeof(default_Option.operation["other"]) == 'function') {
                row["operation"] += "&nbsp;" + default_Option.operation["other"].call(this, row);
            }
        }
        default_Option.$table.refresh(_result.data);
        $("#pageTotal").html(_result.data.total);
    },
    rowDeleteFn: function (_id) {

        if (confirm("您确定要删除吗？")) {

            var url = default_Option.deleteURL,
                data = {id: _id},
                successFun = function (result) {	//设置默认删除成功方法
                    if (result.type == "success") {

                        if (default_Option.callback_del(_id, result)) return;
                        default_Option.queryFn();
                        $.message(result);
                    } else {
                        $.message(result);
                    }
                }

            $.post(url, data, successFun, "json");
        }
    },
    callback_del: function (_id, result) {
    },
    addURL: "",
    deleteURL: "",
    editURL: "",
    execCmd: function () {
        this.queryFn();
    }
}

function initTable(_option) {

    var option = $.extend(true, {}, default_Option),
        option = $.extend(true, option, _option),
        $table = QTable.init(option.table_Id, option.table_Option),
        $search = QSearch.init(option.searchURL, option.searchType);

    option.$search = $search;
    option.$table = $table;

    $search.onSearch(option.successFn);

    $table.refreshHead(option.head);

    $table.onOrderBy(function (_col) {
        if (_col == "operation") return;
        $search.clear();
        option.queryFn();
    });

    $table.onPageSkip(function (p) {
        $search.clear();
        option.queryFn();
    });	//页面跳转事件

    $table.onSelectedChange(function () {	//行选择更改事件
        if (this.getSelected().length > 0) {
            $("#QBar_Del").removeClass("disabled");
        } else {
            $("#QBar_Del").addClass("disabled");
        }
    });

    if (option.addURL) {
        $("#QBar_Add").attr("href", option.addURL);	//添加按钮事件
        $("#QBar_Add").attr("href", option.addURL);	//添加按钮事件
    }

    $("#QBar_Del").click(function () {	//删除按钮事件
        var $this = $(event.currentTarget);
        if ($this.hasClass("disabled")) {
            return false;
        } else {
            $.dialog({
                type: "warn",
                content: "您确定要删除吗？",
                ok: "确定",
                cancel: "取消",
                onOk: function () {

                    var table = QTable.get("#listTable"),
                        url = option.deleteURL,
                        data = {id: table.getSelected().join(",")},
                        successFun = function (result) {	//设置默认删除成功方法
                            if (result.type == "success") {
                                option.queryFn();
                                $("#deleteButton").addClass("disabled");	//失效删除按钮
                                $.message(result);
                            } else {
                                $.message(result);
                            }
                        };

                    $.post(url, data, successFun, "json");
                }
            });
        }
    });

    $("#QBar_Refresh").click(function () {
        $search.clear();
        option.queryFn();
    });
    $("#QBar_Query").click(function () {
        $search.clear();
        option.queryFn();
    });
    $("#QBar_Text").keypress(function () {
        if (event.keyCode == 13) {
            $search.clear();
            option.queryFn();
        }
    });
    default_Option = option;
    option.execCmd();
}